package club.emergency.wechat.authorization.operate.provider.impl;

import club.emergency.wechat.authorization.operate.AuthorizationOperate;
import club.emergency.wechat.common.response.ResponseEntity;
import club.emergency.wechat.common.response.StatusCode;
import club.emergency.wechat.model.authorization.AuthorizationHasRole;
import club.emergency.wechat.model.role.Role;
import club.emergency.wechat.model.role.RoleHasManager;
import club.emergency.wechat.persistence.authorization.AuthorizationHasRoleMapper;
import club.emergency.wechat.persistence.role.RoleHasManagerMapper;
import club.emergency.wechat.persistence.role.RoleMapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Objects;

/**
 * Create with IntelliJ IDEA
 * Project Name: wechat
 * Package Name: club.emergency.wechat.authorization.operate.provider.impl
 * Date: 2019/3/19
 *
 * @author: 快乐的小菌菌
 * Description:
 */
@RestController
public class AuthorizationOperateImpl implements AuthorizationOperate {

    private RoleMapper roleMapper;

    private AuthorizationHasRoleMapper authorizationHasRoleMapper;

    private RoleHasManagerMapper roleHasManagerMapper;

    public AuthorizationOperateImpl(RoleMapper roleMapper, AuthorizationHasRoleMapper authorizationHasRoleMapper, RoleHasManagerMapper roleHasManagerMapper) {
        this.roleMapper = roleMapper;
        this.authorizationHasRoleMapper = authorizationHasRoleMapper;
        this.roleHasManagerMapper = roleHasManagerMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String saveRole(@RequestBody Role role, @RequestParam(value = "authorizationIds", required = false) String authorizationIds) {
        Integer count = roleMapper.checkOnlyForSave(role.getRoleName());
        if (!count.equals(0)) {
            return ResponseEntity.ok(StatusCode.DB_SAVE_OK, "添加角色失败！已有重复的角色名！");
        }
        roleMapper.insert(role);
        Long id = role.getId();
        saveAuthorizationHasRollForLoop(authorizationIds, id);
        return ResponseEntity.ok(StatusCode.DB_SAVE_OK, "添加角色成功！", id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String editRole(@RequestBody Role role, @RequestParam(value = "authorizationIds", required = false) String authorizationIds) {
        Integer count = roleMapper.checkOnlyForEdit(role.getRoleName(), role.getId());
        if (!count.equals(0)) {
            return ResponseEntity.ok(StatusCode.DB_SAVE_OK, "修改角色失败！已有重复的角色名！");
        }
        roleMapper.updateById(role);
        Long id = role.getId();
        if (!Objects.isNull(authorizationIds)) {
            authorizationHasRoleMapper.removeByRoleId(role.getId());
            saveAuthorizationHasRollForLoop(authorizationIds, id);
        }
        return ResponseEntity.ok(StatusCode.DB_MODIFY_OK, "编辑角色成功！");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String removeRole(@RequestParam("id") Long id) {
        roleMapper.removeById(id);
        authorizationHasRoleMapper.removeByRoleId(id);
        return ResponseEntity.ok(StatusCode.DB_REMOVE_OK, "删除的角色成功！");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String bindManager(@RequestParam("managerId") Long managerId, @RequestParam("roleIds") String roleIds) {
        roleHasManagerMapper.removeByManagerId(managerId);
        Arrays.asList(roleIds.split(",")).forEach(roleId -> {
            RoleHasManager roleHasManager = new RoleHasManager();
            roleHasManager.setRoleId(Long.valueOf(roleId));
            roleHasManager.setManagerId(managerId);
            roleHasManagerMapper.insert(roleHasManager);
        });
        return ResponseEntity.ok(StatusCode.DB_SAVE_OK, "后台用户绑定角色成功！");
    }

    @Transactional(rollbackFor = Exception.class)
    public void saveAuthorizationHasRollForLoop(@RequestParam(value = "authorizationIds", required = false) String authorizationIds, Long id) {
        Arrays.asList(authorizationIds.split(",")).forEach(authorizationId -> {
            AuthorizationHasRole authorizationHasRole = new AuthorizationHasRole();
            authorizationHasRole.setRoleId(id);
            authorizationHasRole.setAuthorizationId(Long.valueOf(authorizationId));
            authorizationHasRoleMapper.insert(authorizationHasRole);
        });
    }

}
